package cn.kgc.shiro03.controller;

import cn.kgc.shiro03.dto.RolePageDTO;
import cn.kgc.shiro03.entity.Role;
import cn.kgc.shiro03.service.RolePersService;
import cn.kgc.shiro03.service.RoleService;
import cn.kgc.shiro03.util.CodeMsg;
import cn.kgc.shiro03.util.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/22
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("all")
    public ResponseResult  roleList(){
        List<Role> list = roleService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setData(list);
        return  responseResult;
    }


    @RequestMapping("page")
    public ResponseResult  pageList(RolePageDTO rolePageDTO){
        ResponseResult responseResult = roleService.selectPage(rolePageDTO);
        return  responseResult;
    }

    @RequestMapping("saveOrUpdate")
    public ResponseResult  addRole(Role role){
        ResponseResult responseResult = new ResponseResult();
        // 验证角色名是否已经存在
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name", role.getRoleName());
        Role rl = roleService.getOne(roleQueryWrapper);
        Integer roleId = role.getRoleId();
        if( rl==null || roleId!=null){
            // 添加完成后自动封装自增主键
            roleService.saveOrUpdate(role);

            if(roleId==null){
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
            }else{
                responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
                responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
            }
        }else {
            responseResult.setCode(CodeMsg.ROLE_EXIST.getCode());
            responseResult.setMsg(CodeMsg.ROLE_EXIST.getMsg());
        }
        return  responseResult;
    }

    @RequestMapping("selectById")
    public ResponseResult  selectById(Integer id){

        ResponseResult responseResult = new ResponseResult();

        Role role = roleService.getById(id);
        responseResult.setData(role);
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());

        return responseResult;
    }


    @RequestMapping("deleteById")
    public ResponseResult deleteById(String ids){
        ResponseResult responseResult = roleService.deleteByIds(ids);
        return responseResult;
    }

    //selectPermissionByRoleId   根据角色id 查询权限id  用于权限树数据默认选中
    @RequestMapping("selectPermissionByRoleId")
    public  ResponseResult selectPermissionByRoleId(Integer roleId){
        ResponseResult responseResult = roleService.selectPermissionByRoleId(roleId);
        return  responseResult;
    }


    @RequestMapping("addRolePermission")
    public  ResponseResult addRolePermission(Integer roleId,String permissionIds){
        ResponseResult responseResult = roleService.addRolePermission(roleId,permissionIds);
        return  responseResult;
    }

}
